<script setup>
import {login} from '@/api/user.js'
import {useTokenStore} from '@/stores/token.js'
const useStore =useTokenStore()
import {UserFilled, Lock} from '@element-plus/icons-vue'
import { ref, watch } from 'vue'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'

// 2. 在 setup 中调用
const router = useRouter()
const form = ref()
const   formModel = ref ({
  username: '',
  password: '',
  repassword:'',
})
const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 10, message: '用户名必须是 5-10位 的字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    {  pattern: /^\S{6,15}$/, message: '密码必须是 6-15位 的非空字符', trigger: 'blur' }
  ],
  repassword: [    
    { required: true, message: '请再次输入密码', trigger: 'blur' },
    {  pattern: /^\S{6,15}$/, message: '密码必须是 6-15位 的非空字符', trigger: 'blur' },
    { validator: (rule, value, callback) => {
        // 判断 value 和 当前 form 中收集的 password 是否一致
        if (value !== formModel.value.password) {
          callback(new Error('两次输入密码不一致'))
        } else {
          callback() // 就算校验成功，也需要callback
        }
      }, trigger: 'blur' }
  ]
}
const isRegister = ref(true)
const register = async () => {
  // 注册成功之前，先进行校验，校验成功 → 请求，校验失败 → 自动提示
  await form.value.validate()
  await userRegisterService(formModel.value)
  ElMessage.success('注册成功')
  isRegister.value = false
}

const handleLogin  = async () => {
  await form.value.validate()
  const res = await login(formModel.value)
  useStore.setToken(res.data.token)
  ElMessage.success('登录成功')
  router.push('/')
}

// 切换的时候，重置表单内容
watch(isRegister, () => {
  formModel.value = {
    username: '',
    password: '',
    repassword: ''
  }
})
</script>

<template>
   <!-- //登录表单 -->
    <el-row class="login-page">
      <el-col :span="12" class="bg">
       
      </el-col>
      <el-col :span="6" :offset="3"  class="form">
        <el-form 
        :model="formModel"
        :rules="rules"
        ref="form"
        size="large"
        autocomplete="off"
        v-if="isRegister">
          <el-form-item >
      <h1>登录</h1>
 
    </el-form-item>
    <el-form-item prop="username" >

      <el-input
      v-model="formModel.username"
        name="username"
       size="large"
      placeholder="请输入用户名"
      :prefix-icon="UserFilled "
    />
    
    </el-form-item>
    <el-form-item prop="password"  >
      <el-input
      v-model="formModel.password"
      name="password"
      placeholder="请输入密码"
      :prefix-icon="Lock"
      type="password" 
    />

</el-form-item>
<el-form-item class="flex" >
  <div class="flex">
            <el-checkbox>记住我</el-checkbox>
            <el-link type="primary" :underline="false">忘记密码？</el-link>
          </div>
</el-form-item>

<el-form-item >
  <el-button type="primary" class="button" @click='handleLogin '>登录</el-button>
  

</el-form-item>
<el-form-item >
  <el-link type="info" @click="isRegister = false">注册</el-link>
</el-form-item>
  </el-form>

  <el-form
        :model="formModel"
        :rules="rules"
        ref="form"
        size="large"
        autocomplete="off"
        v-else
      >
     <!-- 注册表单 -->
  <el-form-item >
      <h1>注册</h1>
 
    </el-form-item>
    <el-form-item prop="username">

      <el-input
      v-model="formModel.username"
        name="username"
       size="large"
      placeholder="请输入用户名"
      :prefix-icon="UserFilled "
    />
    
    </el-form-item>
    <el-form-item prop="password" >
      <el-input
      v-model="formModel.password"
      name="password"
      placeholder="请输入密码"
      :prefix-icon="Lock"
      type="password" 
    />

</el-form-item>
<el-form-item  prop="repassword"  >
      <el-input
      v-model="formModel.repassword"
      name="password"
      placeholder="请再次输入密码"
      :prefix-icon="Lock"
      type="password" 
    />

</el-form-item>

<el-form-item >
  <el-button type="primary" class="button"  @click="register">注册</el-button>
</el-form-item>

<el-form-item >
  <el-link type="info" @click="isRegister = true">返回</el-link>
</el-form-item>
  </el-form>

      </el-col>
    </el-row>
 
 


    
  </template>
  
  <style lang="scss" scoped>
 .login-page{
    height: 100vh;
    background-color: yellow;
 
  .bg{
    background: 
    url('@/assets/logo2.png') no-repeat center center, /* 居中不重复显示 */
    url('@/assets/login_bg.jpg') no-repeat center center;  /* 居中不重复显示 */
   
  }
  .form {
    display: flex;
    flex-direction: column;
    justify-content: center;
    user-select: none;
    .flex {
      width: 100%;
      display: flex;
      justify-content: space-between;
    }
    .button{
      width: 100%;
    }
}
 }

  </style>
  
